//
//  SWJoinableGroupCell.swift
//  MiU
//
//  Created by miss on 15/12/10.
//  Copyright © 2015年 MiU. All rights reserved.
//

import Foundation
class SWGroupCell: UIView,SWALPhotoDelegate
{
    private let CoverEdgeInsets:UIEdgeInsets = UIEdgeInsetsMake(0, 5, 6, 5)
    static let MinScale:CGFloat = 0.85
    //
    var scale:CGFloat = 1
    {
        didSet
        {
            updateView()
        }
    }
    
    var data:Any?
    {
        didSet
        {
            if let data = data
            {
                if let url = data as? String
                {
                    _coverImageView.load(url)
                }
                else if let image = data as? UIImage
                {
                    _coverImageView.image = image
                }
                else if let photo = data as? SWALPhoto
                {
                    photo.delegate = self
                    photo.fetchThumbnail()
                }
            }
        }
    }
    
    var focus:Bool = false
    {
        didSet
        {
            if focus != oldValue
            {
                //_titleLabel.font = UIFont.systemFontOfSize(focus ? 11 : 9)
            }
        }
    }
    
    private var _originSize:CGSize = CGSizeZero
    private var _coverImageView:ImageLoader = ImageLoader(frame: CGRectZero)
    
    //private var _bgView:UIImageView = UIImageView(image: UIImage(named:"group_joinable_cell_bg")?.resizableImageWithCapInsets(UIEdgeInsetsMake(7, 7, 7, 7), resizingMode: UIImageResizingMode.Stretch))
    
    
    init(frame: CGRect, originSize:CGSize)
    {
        _originSize = originSize
        super.init(frame: frame)
        setup()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func photoThumbChanged(_ photo:SWALPhoto, thumb:UIImage)
    {
        _coverImageView.image = thumb
    }
    
    private func setup()
    {
        //_bgView.frame = CGRectMake(0, 0, _originSize.width, _originSize.height)
        //addSubview(_bgView)
        
        //封面
        _coverImageView.options.contentMode = .scaleAspectFill
        _coverImageView.clipsToBounds = true
        addSubview(_coverImageView)
        
        
        updateView()
    }
    
    private func updateView()
    {
        let bgWidth:CGFloat = _originSize.width * scale
        let bgHeight:CGFloat = _originSize.height * scale
        let bgX:CGFloat = (width - bgWidth) * 0.5
        let bgY:CGFloat = (height - bgHeight) * 0.5
        _coverImageView.frame = CGRectMake(bgX, bgY, bgWidth, bgHeight)
        //_bgView.frame = _coverImageView.frame
    }
    
    
}
